package com.example.base.adapter.decoration

import android.content.Context
import android.graphics.Canvas
import android.graphics.Paint
import android.graphics.Rect
import android.util.TypedValue
import android.view.View
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView

class DividerItemDecoration(context: Context, orientation: Int, private val mColor: Int, private val isDrawEnd:Boolean =true) :
    RecyclerView.ItemDecoration() {

    private var mOrientation = LinearLayoutManager.VERTICAL

    /**
     * item之间分割线的size，默认为1
     */
    private var mItemSize = 0.5f

    /**
     * 绘制item分割线的画笔，和设置其属性 来绘制个性分割线
     */
    private val mPaint: Paint

    init {
        this.mOrientation = orientation

        if (orientation != LinearLayoutManager.VERTICAL && orientation != LinearLayoutManager.HORIZONTAL) {
            throw IllegalArgumentException("LinearLayoutManager.VERTICAL or LinearLayoutManager.HORIZONTAL please.")
        }
        mItemSize = TypedValue.applyDimension(
            TypedValue.COMPLEX_UNIT_DIP,
            mItemSize,
            context.resources.displayMetrics
        )
        mPaint = Paint(Paint.ANTI_ALIAS_FLAG)
        mPaint.color = mColor
        /* 设置填充 */
        mPaint.style = Paint.Style.FILL
    }

    override fun onDraw(c: Canvas, parent: RecyclerView, state: RecyclerView.State) {
        if (mOrientation == LinearLayoutManager.VERTICAL) {
            drawVertical(c, parent)
        } else {
            drawHorizontal(c, parent)
        }
    }

    /**
     * 绘制纵向 item 分割线
     *
     * @param canvas
     * @param parent
     */
    private fun drawVertical(canvas: Canvas, parent: RecyclerView) {
        val left = parent.paddingLeft
        val right = parent.measuredWidth - parent.paddingRight
        val count =  if (isDrawEnd)  parent.childCount else  parent.childCount-1
        for (i in 0 until count) {
            val child = parent.getChildAt(i)
            val layoutParams = child.layoutParams as RecyclerView.LayoutParams

            val top = child.bottom + layoutParams.bottomMargin
            val bottom = top + mItemSize
            mPaint.color = mColor
            canvas.drawRect(left.toFloat(), top.toFloat(), right.toFloat(), bottom, mPaint)

            //			mPaint.setColor(0x66ffffff);
            //			canvas.drawRect(left, top + 1, right, bottom, mPaint);
        }
    }

    /**
     * 绘制横向 item 分割线
     *
     * @param canvas
     * @param parent
     */
    private fun drawHorizontal(canvas: Canvas, parent: RecyclerView) {
        val top = parent.paddingTop
        val bottom = parent.measuredHeight - parent.paddingBottom
        val childSize = parent.childCount
        for (i in 0 until childSize - 1) {
            val child = parent.getChildAt(i)
            val layoutParams = child.layoutParams as RecyclerView.LayoutParams
            val left = child.right + layoutParams.rightMargin
            val right = left + mItemSize
            canvas.drawRect(left.toFloat(), top.toFloat(), right, bottom.toFloat(), mPaint)
        }
    }

    /**
     * 设置item分割线的size
     *
     * @param outRect
     * @param view
     * @param parent
     * @param state
     */
    override fun getItemOffsets(outRect: Rect, view: View, parent: RecyclerView, state: RecyclerView.State) {
        if (mOrientation == LinearLayoutManager.VERTICAL) {
            outRect.set(0, 0, 0, (mItemSize+0.5).toInt())
        } else {
            outRect.set(0, 0, (mItemSize+0.5).toInt(), 0)
        }
    }

}